package com.thunder.component;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import com.thunder.utils.Constant;
import com.thunder.utils.GameUtil;

public class UserFly {
	private final BufferedImage image;
	
	private int x;
	private final int y;
	public UserFly() {
		image = GameUtil.loadBufferedImage(Constant.USERFLY_IMG_PATH);
		y = 900 - 124;
		x = 300;
	}
	
	public void draw(Graphics g) {
		g.drawImage(image, x - 50, y, null);
	}
	
	public void move(int dir) {
		if((dir == -1 && x <= 50) || (dir == 1 && x >= 550)) {
			return;
		}
		else {
			x += dir * Constant.delta;
		}
	}
	
	public int beAttack(ArrayList<Bullet> bullet) {
		for(int i = 0; i < bullet.size(); i++) {
			Bullet b = bullet.get(i);
			if(b.getY() > 900) {
				bullet.remove(i);
				continue;
			}
			if(b.getDir() == 1 && x - 50 <= b.getX() && b.getX() <= x + 50) //EFly width=70
			{
				int d0 = Math.abs(b.getX() - x);
				if(b.getY() >= (y + d0)) {
					return 1;
				}
			}
		}
		return 0;
	}
	
	public int getX() {
		return x;
	}
	
	public int getY() {
		return y;
	}
	
}
